package cn.jbt.api.admin.service.impl;

import cn.jbt.api.admin.entity.QSysLoginLog;
import cn.jbt.api.admin.entity.SysLoginLog;
import cn.jbt.api.admin.repository.SysLoginLogRepository;
import cn.jbt.api.admin.service.ISysLoginLogService;
import cn.jbt.common.PageQryParam;
import cn.jbt.common.PageQryResult;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@AllArgsConstructor
public class SysLoginLogServiceImpl implements ISysLoginLogService {

    private final JPAQueryFactory jpaQueryFactory;
    private final SysLoginLogRepository sysLoginLogRepository;

    @Override
    public PageQryResult<SysLoginLog> getAllByPage(PageQryParam<SysLoginLog> pageQryParam) {
        SysLoginLog qParam = pageQryParam.getParam();
        QSysLoginLog qSysLoginLog = QSysLoginLog.sysLoginLog;

        Predicate predicate = qSysLoginLog.isNotNull().or(qSysLoginLog.isNull());
        if (null != qParam) {
            if (!StringUtils.isEmpty(qParam.getUsername())) {
                predicate = ExpressionUtils.and(predicate,
                        qSysLoginLog.username.eq(qParam.getUsername()));
            }
            if (!StringUtils.isEmpty(qParam.getIpAddr())) {
                predicate = ExpressionUtils.and(predicate,
                        qSysLoginLog.ipAddr.eq(qParam.getIpAddr()));
            }
            if (null != qParam.getStatus()) {
                predicate = ExpressionUtils.and(predicate,
                        qSysLoginLog.status.eq(qParam.getStatus()));
            }
            if (null != qParam.getCreateTimeStart()) {
                predicate = ExpressionUtils.and(predicate,
                        qSysLoginLog.createTime.goe(qParam.getCreateTimeStart()));
            }
            if (null != qParam.getCreateTimeEnd()) {
                predicate = ExpressionUtils.and(predicate,
                        qSysLoginLog.createTime.loe(qParam.getCreateTimeEnd()));
            }
        }

        List<SysLoginLog> list = jpaQueryFactory.select(qSysLoginLog)
                .from(qSysLoginLog)
                .where(predicate)
                .orderBy(qSysLoginLog.id.desc())
                .offset((pageQryParam.getPage() - 1) * pageQryParam.getSize())
                .limit(pageQryParam.getSize())
                .fetch();

        long total = Optional.ofNullable(jpaQueryFactory.select(qSysLoginLog.id.count())
                .from(qSysLoginLog)
                .where(predicate)
                .fetchOne()
        ).orElse(0L);

        return new PageQryResult<>(list, total);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void del(List<Long> ids) {
        sysLoginLogRepository.deleteAllByIdInBatch(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delAll() {
        sysLoginLogRepository.deleteAll();
    }
}
